package com.wxx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxx.cache.Cache;
import com.wxx.cache.WeixxCache;
import com.wxx.mapper.WeixxMapper;
import com.wxx.model.Article;
import com.wxx.model.Body;
import com.wxx.model.Weixx;
import com.wxx.service.C1Service;

@Controller
public class C1 {
	private static Logger logger = Logger.getLogger(C1.class);

	@Autowired
	WeixxMapper weixxMapper;

	@Autowired
	WeixxCache weixxCache;

	@Resource
	C1Service c1Service;

	/*
	 * @Autowired WeixxDao weixxDao;
	 */

	// 直接通过mapper接口实现
	@RequestMapping("/wxx/c1")
	@ResponseBody
	public String getC1() {
		Weixx weixx = weixxMapper.selectWeixxById("1");
		logger.info(">>>>>>>>weixxCache>>yy>>>>>>>"
				+ weixxCache.getProperty("yy"));
		logger.info(">>>>>>>>>>>>>>>>>" + weixx.getUsername());
		System.out.println(">>>>>>>>>>>>>>>>>" + weixx.getUsername());
		return weixx.getUsername();
	}

	// 通过标准的MVC结构实现
	@RequestMapping("/wxx/c2")
	@ResponseBody
	public String getC2() {
		Weixx weixx = c1Service.getC1Serv("2");
		return weixx.getUsername();
	}

	// 通过DAO实现
	/*
	 * @RequestMapping("/wxx/c3")
	 * 
	 * @ResponseBody public String getC3() { List<Weixx> weixx =
	 * weixxDao.getWeixx("3"); StringBuffer sb = null; for(Weixx wxx : weixx) {
	 * sb.append("-"+wxx.getUsername()); } return sb.toString();
	 * 
	 * }
	 */

	// 多个参数 利用 Mybatis 自身的多个参数传递方式去做
	@RequestMapping("/wxx/{frage}/{asdasd}")
	@ResponseBody
	public String getC4(@RequestHeader HttpHeaders header,
			@RequestBody String body, HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam Map<String, String> uriVariables,
			@PathVariable String frage, @PathVariable String asdasd) {
		// {accept-encoding=[gzip,deflate], content-type=[application/xml],
		// clentid=[151], userid=[m2weixx], chnflg=[1],
		// acton=[d51f089e-2db2-4db1-8c6b-db6931876660], content-length=[164],
		// host=[localhost:8980], connection=[Keep-Alive],
		// user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
		// JSONObject aa = JSONObject.fromObject(frage);
		// List<String> clientId = header.get("clentid");
		Weixx weixx = c1Service.getC1Serv("2", "pp");
		return weixx.getUsername();
	}

	// 多个参数 利用 hashMap 传递多个参数
	@RequestMapping("/wxx/c5")
	@ResponseBody
	public String getC5() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", "1");
		map.put("username", "rr");
		Weixx weixx = c1Service.getC1Serv(map);
		return weixx.getUsername();
	}

	// 用 association 来得到关联的用户，这是多对一的情况
	@RequestMapping("/wxx/c6")
	@ResponseBody
	public String getC6() {

		List<Article> Articles = c1Service.getArticles("1");
		StringBuffer sb = new StringBuffer();
		for (Article article : Articles) {
			sb.append(">>" + article.getContent());
			System.out.println(">>>>4>>>>" + article.getContent());
		}
		return sb.toString();
	}

	// 将 association 中对应的映射独立抽取出来，可以达到复用的目的
	@RequestMapping("/wxx/c7")
	@ResponseBody
	public String getC7() {

		List<Article> Articles = c1Service.getArticles2("1");
		StringBuffer sb = new StringBuffer();
		for (Article article : Articles) {
			sb.append("aaaaaaaaaaaaaaaa>>" + article.getWeixx().getUsername());
			System.out.println(">>>>4>>>>" + article.getWeixx().getUsername());
		}
		return sb.toString();
	}

	@RequestMapping("/wxx/c8")
	@ResponseBody
	public void add() {

		Weixx weixx = new Weixx();
		weixx.setId("Weixx");
		weixx.setUserid("Moer");
		weixx.setUsername("Jing");
		c1Service.add(weixx);
	}

	@RequestMapping("/wxx/c9")
	@ResponseBody
	public void update() {
		Weixx weixx = new Weixx();
		weixx.setId("Weixx");
		weixx.setUserid("Moer");
		weixx.setUsername("eeeeee");
		c1Service.update(weixx);
	}

	@RequestMapping("/wxx/c10")
	@ResponseBody
	public void delete() {
		c1Service.delete("1");
	}

	/*
	 * 上送中包含对象的话有两种场景
	 * 1 在方法入参中直接传入对象 前端上送时要不要设置contentType或设置成x-www-form-unencoded
	 * 2  在方法入参中@RequestBody+传入对象 前端上送时在body中上送 要设置contentType或设置成application/json 
	 */
	@RequestMapping(value = "/wxx/c11", method = RequestMethod.POST)
	@ResponseBody
	// @RequestBody String body,
	public String getC11(@RequestBody Body body1, @RequestHeader HttpHeaders header,
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, String> uriVariables) {
		logger.info("+++++++++++++++++++++++" + body1.getMethod());
		// logger.setLevel(Level.WARN);
		logger.trace("Trace Message!");
		logger.debug("Debug Message!");
		logger.info("Info Message!");
		logger.warn("Warn Message!");
		logger.error("Error Message!");
		logger.fatal("Fatal Message!");
		Weixx weixx = c1Service.getC1Serv("2", "pp");
		System.out.println(">>>>>>>>>>>>>>>>>" + weixx.getUsername());
		logger.info(">>>>>>>>>>>>>>>>>" + weixx.getUsername());
		return weixx.getUsername();
	}

	@RequestMapping(value = "/wxx/c12", method = RequestMethod.POST)
	@ResponseBody
	// @RequestBody String body,
	public String getC12(Body body1, @RequestHeader HttpHeaders header,
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, String> uriVariables) {
		Map<String, String> wMap = Cache.getWxxMap();
		System.out.println("wMap>>>>>>>>" + wMap.toString());
		// Map<String, String> sss=Cache1.weixxMap;
		// String str = Cache2.str;
		return wMap.toString();
	}

	@RequestMapping(value = "/wxx/c13", method = RequestMethod.POST)
	@ResponseBody
	// @RequestBody String body,
	public String getC13(Body body1, @RequestHeader HttpHeaders header,
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, String> uriVariables) {
		Map<String, String> wMap = Cache.getWxxMap();
		System.out.println("wMap>>>>>>>>" + wMap.toString());
		// Map<String, String> sss=Cache1.weixxMap;
		// String str = Cache2.str;
		// ApplicationContext acx = new
		// ClassPathXmlApplicationContext("applicationContext.xml");
		// Object ob = acx.getBean("filter");
		return wMap.toString();
	}

}
